package com.ask.langchain4j_springboot.controller;

import com.ask.langchain4j_springboot.config.ChatAssistantConfig;
import dev.langchain4j.service.TokenStream;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/refund")
@Tag(name = "退票助手接口")
public class RefundAssistantController {
    @Autowired
    private ChatAssistantConfig.RefundAssistant assistant;

    @GetMapping(value = "/refund", produces = "text/stream;charset=UTF-8")
    @Operation(summary = "票务助手")
    public Flux<String> memoryStreamChat1(@RequestParam(defaultValue = "你好，我想退票") String message, HttpServletResponse response) {
        TokenStream stream = assistant.stream(message);

        return Flux.create(sink -> {
            stream.onPartialResponse(s -> sink.next(s))
                    .onCompleteResponse(c -> sink.complete())
                    .onError(sink::error)
                    .start();

        });
    }
}
